package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.CommonDict;
import entities.TIntercept;
import interfaces.IInterceptService;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;

public class InterceptAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private IInterceptService interceptService;

	private TIntercept interceptBean;
	private List<TIntercept> interceptList;

	private String incNo;
	private String incNos;
	private String incOrder;
	private String incWebsite;
	private String incInfo;
	private Date incDate;
	private Date incDateTop;
	private Date incDateEnd;
	private String incRemark;

	// 展示对象
	public String show() throws Exception {
		if (!interceptService.exist(incNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			interceptBean = interceptService.fetch(incNo);
			return SUCCESS;
		}
	}

	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("incDefault", ActionContext.getContext().getSession().get("query").toString());
		paramMap.put("incNo", StringUtil.split(incNo));
		paramMap.put("incOrder", incOrder);
		paramMap.put("incWebsite", incWebsite);
		paramMap.put("incInfo", incInfo);
		paramMap.put("incDate", ParameterUtil.dateCombin(incDateTop, incDateEnd));
		paramMap.put("incRemark", incRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "incDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TIntercept> resultSet = interceptService.search(querySet);
		interceptList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("incDefault", ActionContext.getContext().getSession().get("query").toString());
		paramMap.put("incNo", StringUtil.split(incNo));
		paramMap.put("incOrder", incOrder);
		paramMap.put("incWebsite", incWebsite);
		paramMap.put("incInfo", incInfo);
		paramMap.put("incDate", ParameterUtil.dateCombin(incDateTop, incDateEnd));
		paramMap.put("incRemark", incRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "incDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TIntercept> resultSet = interceptService.search(querySet);
		interceptList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("订单号");
		fieldHead.add("网站");
		fieldHead.add("拦截原因");
		fieldHead.add("日期日期");
		fieldHead.add("日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TIntercept interceptBean : interceptList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(interceptBean.getIncNo());
			fieldItem.add(interceptBean.getIncOrder());
			fieldItem.add(interceptBean.getIncWebsite());
			fieldItem.add(interceptBean.getIncInfo());
			fieldItem.add(DateUtil.format(interceptBean.getIncDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(interceptBean.getIncDate(), DateUtil.TIME));
			fieldItem.add(interceptBean.getIncRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IInterceptService getInterceptService() {
		return interceptService;
	}

	public void setInterceptService(IInterceptService interceptService) {
		this.interceptService = interceptService;
	}

	public TIntercept getInterceptBean() {
		return interceptBean;
	}

	public void setInterceptBean(TIntercept interceptBean) {
		this.interceptBean = interceptBean;
	}

	public List<TIntercept> getInterceptList() {
		return interceptList;
	}

	public void setInterceptList(List<TIntercept> interceptList) {
		this.interceptList = interceptList;
	}

	public String getIncNo() {
		return incNo;
	}

	public void setIncNo(String incNo) {
		this.incNo = incNo;
	}

	public String getIncNos() {
		return incNos;
	}

	public void setIncNos(String incNos) {
		this.incNos = incNos;
	}

	public String getIncOrder() {
		return incOrder;
	}

	public void setIncOrder(String incOrder) {
		this.incOrder = incOrder;
	}

	public String getIncWebsite() {
		return incWebsite;
	}

	public void setIncWebsite(String incWebsite) {
		this.incWebsite = incWebsite;
	}

	public String getIncInfo() {
		return incInfo;
	}

	public void setIncInfo(String incInfo) {
		this.incInfo = incInfo;
	}

	public Date getIncDate() {
		return incDate;
	}

	public void setIncDate(Date incDate) {
		this.incDate = incDate;
	}

	public Date getIncDateTop() {
		return incDateTop;
	}

	public void setIncDateTop(Date incDateTop) {
		this.incDateTop = incDateTop;
	}

	public Date getIncDateEnd() {
		return incDateEnd;
	}

	public void setIncDateEnd(Date incDateEnd) {
		this.incDateEnd = incDateEnd;
	}

	public String getIncRemark() {
		return incRemark;
	}

	public void setIncRemark(String incRemark) {
		this.incRemark = incRemark;
	}

}
